package cac.fenjian.resource.mswptoolbasic.rest;

import cac.fenjian.resource.MsWpTool.dto.MsWpToolBasicDTO;
import cac.fenjian.resource.mswptoolbasic.service.MsWpToolBasicService;
import com.alibaba.fastjson.JSON;
import com.cac.dme.adapter.rest.msg.PageParameter;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.adapter.utils.ContextCommonHolder;
import com.cac.dme.common.utils.JsonHelper;
import com.fasterxml.jackson.core.type.TypeReference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.cac.dme.adapter.utils.BusinessUtil.getSortExpColumnName;

@RestController
@Api(tags = "工作包器具接口")
@RequestMapping("/MsWpTool")
public class MsWpToolBasicRest {

    @Autowired
    private MsWpToolBasicService msWpToolBasicService;

    /**
     * 按条件分页查询
     *
     * @param map
     * @return
     */
    @PostMapping("/searchByPage/v1")
    @ApiOperation(value = "按条件分页查询")
    public ResponseMsg<QueryRespBean<MsWpToolBasicDTO>> searchByPage(@ApiParam(value = "查询条件", name = "queryReqBean") @RequestBody HashMap map) {
        ResponseMsg<QueryRespBean<MsWpToolBasicDTO>> responseMsg = new ResponseMsg<>();
        //分页
        String param = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(param, PageParameter.class);
        //查询条件
        String searchParamsParams = JSON.toJSONString(map.get("searchParams"));
        MsWpToolBasicDTO searchParams = JSON.parseObject(searchParamsParams, MsWpToolBasicDTO.class);

        QueryReqBean<MsWpToolBasicDTO> queryReqBean = new QueryReqBean<>();
        queryReqBean.setSearchParams(searchParams);
        queryReqBean.setPageParameter(pageParameter);

        if (StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())) {
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(), queryReqBean.getSord(), MsWpToolBasicDTO.class);
            if (StringUtils.hasText(sordExp)) {
                queryReqBean.setSortExp(sordExp);
            }
        }
        if (StringUtils.hasText(queryReqBean.getKeyWord())) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            MsWpToolBasicDTO searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<MsWpToolBasicDTO>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }

        QueryRespBean<MsWpToolBasicDTO> result = msWpToolBasicService.searchMsWpToolBasicByPage(queryReqBean, ContextCommonHolder.getOrgId(),queryReqBean.getSortExp());
        List<MsWpToolBasicDTO> result1 = msWpToolBasicService.searchMsWpToolBasicByPage(searchParams, ContextCommonHolder.getOrgId(),queryReqBean.getSortExp());
        if(result1.size()==0){
            responseMsg.setRetCode("201").setMessage("未查询到数据").setResponseBody(result);
        }
        else responseMsg.setResponseBody(result);
        return  responseMsg;
    }

    /**
     * 批量新增数据
     *
     * @param
     * @return
     */
    @PostMapping("/saveMany/v1")
    @ApiOperation(value = "批量新增数据")
    public ResponseMsg<Map> saveMany(@ApiParam(value = "待新增数据集", name = "msWpToolBasicDTOS") @RequestBody Map map) throws ParseException {
        ResponseMsg<Map> responseMsg = new ResponseMsg<>();
        int s = 0;
        int f = 0;
        try {
            msWpToolBasicService.insertMsWpToolBasic(map);
            s++;
        } catch (Exception e) {
            e.printStackTrace();
            f++;
        }
        if (s == 0 && f != 0) {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("新增失败！");

        }
        if (s != 0 && f == 0) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("新增成功！");
        }
        if (s != 0 && f != 0) {
            responseMsg.setRetCode("202");
            responseMsg.setMessage("新增有失败项！");
        }
        return responseMsg;
    }

    /**
     * 批量修改数据
     *
     * @param
     * @return
     */
    @PostMapping("/updateMany/v1")
    @ApiOperation(value = "修改数据")
    public ResponseMsg<Map> updateMany(@ApiParam(value = "需要修改数据集", name = "msWpToolBasicDTOS") @RequestBody Map map) {
        ResponseMsg<Map> responseMsg = new ResponseMsg<>();
        int s = 0;
        int f = 0;
        if (map.get("msWpToolId") != null) {
            try {
                msWpToolBasicService.updateMsWpToolMany(map);
                s++;
            } catch (Exception e) {
                e.printStackTrace();
                f++;
            }
        } else f++;
        if (s == 0 && f != 0) {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("修改失败！");

        }
        if (s != 0 && f == 0) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("修改成功！");
        }
        if (s != 0 && f != 0) {
            responseMsg.setRetCode("202");
            responseMsg.setMessage("修改有失败项！");
        }
        return responseMsg;
    }

    /**
     * 删除数据
     *
     * @param
     * @return
     */
    @PostMapping("/deleteMany/v1")
    @ApiOperation(value = "删除数据")
    public ResponseMsg<Map> deleteMany(@ApiParam(value = "被删数据主键ID集", name = "ids") @RequestBody HashMap<String, Object> map) {
        ResponseMsg<Map> responseMsg = new ResponseMsg<>();
        Map reMsg = new HashMap();
        int s = 0;
        int f = 0;
        List<HashMap<String, Object>> ids = (List<HashMap<String, Object>>) map.get("data");
        for (int i = 0; i < ids.size(); i++) {
            HashMap<String, Object> map1 = ids.get(i);
            String id = (String) map1.get("msWpToolId");
            if (map1.get("msWpToolId") != null) {
                msWpToolBasicService.deleteByMsWpToolBasicDTOById(id);
                s++;
            } else f++;
        }
        reMsg.put("成功：", s);
        reMsg.put("失败：", f);
        if (s == 0 && f != 0) {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("删除失败！");

        }
        if (s != 0 && f == 0) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("删除成功！");
        }
        if (s != 0 && f != 0) {
            responseMsg.setRetCode("202");
            responseMsg.setMessage("删除有失败项！");
            responseMsg.setResponseBody(reMsg);
        }
        return responseMsg;
    }

}
